package com.cooba.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.SignBean;
import com.cooba.views.choicecity.CityPicker;

/**
 * Created by Administrator on 2016/6/15.
 */
public class SignDialog extends Dialog implements
        OnClickListener {
    int layoutRes;// 布局文件
    Context context;
    private TextView mSignNum;//连续签到
    private TextView mSignTotal;//签到总次
    private TextView mSignTotalIntegral;//签到总积分
    private TextView mSignRule;//签到规则
    private ImageView mSignBtn;
    private DialogListener listener;
    private SignBean signBean;


    public SignDialog(Context context) {
        super(context);
        this.context = context;
    }

    /**
     * 自定义布局的构造方法
     *
     * @param context
     * @param resLayout
     */
    public SignDialog(Context context, int resLayout) {
        super(context);
        this.context = context;
        this.layoutRes = resLayout;
    }

    /**
     * 自定义主题及布局的构造方法
     *
     * @param context
     * @param theme
     * @param resLayout
     */
    public SignDialog(Context context, int theme, int resLayout, SignBean signBean,
                      DialogListener listener) {
        super(context, theme);
        this.context = context;
        this.layoutRes = resLayout;
        this.listener = listener;
        this.signBean = signBean;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(layoutRes);

        // 根据id在布局中找到控件对象
        mSignNum = (TextView) findViewById(R.id.signNum_tv);
        mSignTotal = (TextView) findViewById(R.id.signTotal_tv);
        mSignTotalIntegral = (TextView) findViewById(R.id.signIntegralTotal_tv);
        mSignBtn = (ImageView) findViewById(R.id.sign_btn);
        mSignRule = (TextView) findViewById(R.id.sign_rule_tv);
        mSignBtn.setOnClickListener(this);
        init();
    }

    private void init() {
        String signnum = null;
        String integraltotal = null;
        String signtotal = null;
        if (signBean.getSignNum()==null){
            signnum = "0";
        }else {
            signnum=signBean.getSignNum();
        }
        if (signBean.getSignTotal()==null){
            signtotal = "0";
        }else {
            signtotal=signBean.getSignTotal();
        }
        if (signBean.getIntegralTotal()==null){
            integraltotal = "0";
        }else {
            integraltotal=signBean.getIntegralTotal();
        }
        mSignNum.setText(context.getString(R.string.sign_num, signnum));
        mSignTotal.setText(context.getString(R.string.sign_total, signtotal));
        mSignTotalIntegral.setText(context.getString(R.string.sign_total_integral, integraltotal));
        int n = Integer.parseInt(signBean.getIncrementItg());
        int m = Integer.parseInt(signBean.getSignCycle());
        String rule = "签到规则\n\n1.每天只签到一次（按服务器系统时间为准）\n\n2.首次签到获得"+n+"积分奖励\n\n3.连续签到每天递增"+n+"积分奖励，例如：第一天"+n+"积分，第二天"+2*n+"积分\n\n4.连续签到"+m+"天及以上每天获得"+m*n+"积分奖励，例如：第"+m+"天"+m*n+"积分，第"+m+"天之后都是"+m*n+"积分，不再递增\n\n5.中断签到，则从头计算积分，例如：连续签到3天，第4天未签到，第5天签到获得"+n+"积分\n\n其他说明\n\n1.签到积分与应用下载获取的积分相同，可兑换海量商品，大家踊跃签到并下载应用\n\n2.最终解释权归本公司所有，对违规获取积分者保留追究责任权利";
        mSignRule.setText(rule);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_btn:
                listener.sign();
                this.dismiss();
                break;

        }
    }

    public interface DialogListener {

        void sign();
    }
}
